
package com.hitqz.robot.camera.callback;

import com.hitqz.robot.camera.service.HCNetSDK;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class HkAlarmMessageCallBack implements HCNetSDK.FMSGCallBack_V31 {
    //报警信息回调函数
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        AlarmDataHandle(lCommand, pAlarmer, pAlarmInfo, dwBufLen, pUser);
        return true;
    }

    public void AlarmDataHandle(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        //log.info("报警事件类型： lCommand:" + Integer.toHexString(lCommand));
//        String sTime;
//        String MonitoringSiteID;
        //lCommand是传的报警类型
        switch (lCommand) {
            case HCNetSDK.COMM_ISAPI_ALARM: //ISAPI协议报警信息
                HCNetSDK.NET_DVR_ALARM_ISAPI_INFO struEventISAPI = new HCNetSDK.NET_DVR_ALARM_ISAPI_INFO();
                struEventISAPI.write();
                Pointer pEventISAPI = struEventISAPI.getPointer();
                pEventISAPI.write(0, pAlarmInfo.getByteArray(0, struEventISAPI.size()), 0, struEventISAPI.size());
                struEventISAPI.read();
                String sAlarmInfo = new String(pAlarmer.sDeviceIP);
                //报警数据类型：0- invalid，1- xml，2- json
                sAlarmInfo = "报警设备IP：" + sAlarmInfo + "：ISAPI协议报警信息, 数据格式:" + struEventISAPI.byDataType +
                        ", 图片个数:" + struEventISAPI.byPicturesNumber;
                System.out.println(sAlarmInfo);

                //报警数据保存
                SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
                String curTime1 = sf1.format(new Date());
                FileOutputStream foutdata;
                try {
                    String jsonfilename = "../pic/" + new String(pAlarmer.sDeviceIP).trim() + curTime1 + "_ISAPI_Alarm_" + ".json";
                    foutdata = new FileOutputStream(jsonfilename);
                    //将字节写入文件
                    ByteBuffer jsonbuffers = struEventISAPI.pAlarmData.getByteBuffer(0, struEventISAPI.dwAlarmDataLen);
                    byte[] jsonbytes = new byte[struEventISAPI.dwAlarmDataLen];
                    jsonbuffers.rewind();
                    jsonbuffers.get(jsonbytes);
                    foutdata.write(jsonbytes);
                    foutdata.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //图片数据保存
                for (int i = 0; i < struEventISAPI.byPicturesNumber; i++) {
                    HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA struPicData = new HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA();
                    struPicData.write();
                    Pointer pPicData = struPicData.getPointer();
                    pPicData.write(0, struEventISAPI.pPicPackData.getByteArray((long) i * struPicData.size(), struPicData.size()), 0, struPicData.size());
                    struPicData.read();

                    FileOutputStream fout;
                    try {
                        String filename = "../pic/" + new String(pAlarmer.sDeviceIP).trim() + curTime1 +
                                "_ISAPIPic_" + i + "_" + new String(struPicData.szFilename).trim() + ".jpg";
                        fout = new FileOutputStream(filename);
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = struPicData.pPicData.getByteBuffer(offset, struPicData.dwPicLen);
                        byte[] bytes = new byte[struPicData.dwPicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            case HCNetSDK.COMM_VCA_ALARM:  // 智能检测通用报警(Json或者XML数据结构)
                sAlarmInfo = new String(pAlarmer.sDeviceIP);
                //报警数据类型：0- invalid，1- xml，2- json
                sAlarmInfo = "报警设备IP：" + sAlarmInfo;
                System.out.println(sAlarmInfo);

                SimpleDateFormat sf0 = new SimpleDateFormat("yyyyMMddHHmmss");
                String curTime0 = sf0.format(new Date());
                FileOutputStream Data;
                String jsonfile = "../pic" + new String(pAlarmer.sDeviceIP).trim() + curTime0 + "_VCA_ALARM_" + ".json";
                try {
                    Data = new FileOutputStream(jsonfile);
                    //将字节写入文件
                    ByteBuffer dataBuffer = pAlarmInfo.getByteBuffer(0, dwBufLen);
                    byte[] dataByte = new byte[dwBufLen];
                    dataBuffer.rewind();
                    dataBuffer.get(dataByte);
                    Data.write(dataByte);
                    Data.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case HCNetSDK.COMM_ALARM_V30:  //移动侦测、视频丢失、遮挡、IO信号量等报警信息(V3.0以上版本支持的设备)
                HCNetSDK.NET_DVR_ALARMINFO_V30 struAlarmInfo = new HCNetSDK.NET_DVR_ALARMINFO_V30();
                struAlarmInfo.write();
                Pointer pAlarmInfo_V30 = struAlarmInfo.getPointer();
                pAlarmInfo_V30.write(0, pAlarmInfo.getByteArray(0, struAlarmInfo.size()), 0, struAlarmInfo.size());
                struAlarmInfo.read();
                log.info("报警类型：" + struAlarmInfo.dwAlarmType);  // 3-移动侦测
                break;
            //行为分析信息
            case HCNetSDK.COMM_ALARM_RULE:
                HCNetSDK.NET_VCA_RULE_ALARM strVcaAlarm = new HCNetSDK.NET_VCA_RULE_ALARM();
                strVcaAlarm.write();
                Pointer pVCAInfo = strVcaAlarm.getPointer();
                pVCAInfo.write(0, pAlarmInfo.getByteArray(0, strVcaAlarm.size()), 0, strVcaAlarm.size());
                strVcaAlarm.read();
                int dwID = strVcaAlarm.struTargetInfo.dwID;
                float fX = strVcaAlarm.struTargetInfo.struRect.fX;
                float fY = strVcaAlarm.struTargetInfo.struRect.fY;
                float fHeight = strVcaAlarm.struTargetInfo.struRect.fHeight;
                float fWidth = strVcaAlarm.struTargetInfo.struRect.fWidth;
                switch (strVcaAlarm.struRuleInfo.wEventTypeEx) {
                    case 1: //穿越警戒面 (越界侦测)
                        log.info("穿越警戒面报警发生");
                        int dwCrossDirection = strVcaAlarm.struRuleInfo.uEventParam.struTraversePlane.dwCrossDirection;
                        log.info("dwCrossDirection:{}",dwCrossDirection);
                        break;
                    case 2: //目标进入区域
                        log.info("dwId:{},fX:{},fY:{},fHeight:{},fWidth:{}",dwID,fX,fY,fHeight,fWidth);
                        log.info("目标进入区域报警发生");
                        break;
                    case 3: //目标离开区域
                        log.info("dwId:{},fX:{},fY:{},fHeight:{},fWidth:{}",dwID,fX,fY,fHeight,fWidth);
                        log.info("目标离开区域报警触发");
                        break;
                    case 4: //周界入侵
                        log.info("dwId:{},fX:{},fY:{},fHeight:{},fWidth:{}",dwID,fX,fY,fHeight,fWidth);
                        log.info("周界入侵报警发生");
                        break;
                    case 5: //徘徊
                        log.info("徘徊事件触发");

                        break;
                    case 8: //快速移动(奔跑)，
                        log.info("快速移动(奔跑)事件触发");
                        break;
                    case 15: //离岗
                        log.info("离岗事件触发");
                    case 20: //倒地检测
                        log.info("倒地事件触发");
                        break;
                    case 44: //玩手机

                        log.info("玩手机报警发生");

//                        if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
//                            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                            String newName = sf.format(new Date());
//                            FileOutputStream fout;
//                            try {
//                                String filename = "../pic/" + newName + "PLAY_CELLPHONE_" + ".jpg";
//                                fout = new FileOutputStream(filename);
//                                //将字节写入文件
//                                long offset = 0;
//                                ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
//                                byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
//                                buffers.rewind();
//                                buffers.get(bytes);
//                                fout.write(bytes);
//                                fout.close();
//                            } catch (FileNotFoundException e) {
//                                // TODO Auto-generated catch block
//                                e.printStackTrace();
//                            } catch (IOException e) {
//                                // TODO Auto-generated catch block
//                                e.printStackTrace();
//                            }
//                        }
                        break;
                    case 45: //持续检测
                        //log.info("持续检测事件触发");
                        break;
                    default:
                        log.info("行为事件类型:"+strVcaAlarm.struRuleInfo.wEventTypeEx);
                        break;
                }
                break;
            case HCNetSDK.COMM_THERMOMETRY_ALARM:  //温度报警信息
                HCNetSDK.NET_DVR_THERMOMETRY_ALARM struTemInfo = new HCNetSDK.NET_DVR_THERMOMETRY_ALARM();
                struTemInfo.write();
                Pointer pTemInfo = struTemInfo.getPointer();
                pTemInfo.write(0, pAlarmInfo.getByteArray(0, struTemInfo.size()), 0, struTemInfo.size());
                struTemInfo.read();
                String sThermAlarmInfo = "规则ID:" + struTemInfo.byRuleID + "预置点号：" + struTemInfo.wPresetNo + "报警等级：" + struTemInfo.byAlarmLevel + "报警类型：" +
                        struTemInfo.byAlarmType + "当前温度：" + struTemInfo.fCurrTemperature;
                System.out.println(sThermAlarmInfo);
                //可见光图片保存
                if ((struTemInfo.dwPicLen > 0) && (struTemInfo.byPicTransType == 0)) {
                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String newName = sf.format(new Date());
                    FileOutputStream fout;

                    try {
                        String filename = "../pic/" + newName + "_" + struTemInfo.fCurrTemperature + ".jpg";
                        fout = new FileOutputStream(filename);
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = struTemInfo.pPicBuff.getByteBuffer(offset, struTemInfo.dwPicLen);
                        byte[] bytes = new byte[struTemInfo.dwPicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
//
//                if ((struTemInfo.dwThermalPicLen > 0) && (struTemInfo.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_" + "_ThermalPiC" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struTemInfo.pThermalPicBuff.getByteBuffer(offset, struTemInfo.dwThermalPicLen);
//                        byte[] bytes = new byte[struTemInfo.dwThermalPicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//
//                }
//                break;
//            case HCNetSDK.COMM_THERMOMETRY_DIFF_ALARM: //温差检测报警
//                HCNetSDK.NET_DVR_THERMOMETRY_DIFF_ALARM strThermDiffAlarm = new HCNetSDK.NET_DVR_THERMOMETRY_DIFF_ALARM();
//                strThermDiffAlarm.write();
//                Pointer pTemDiffInfo = strThermDiffAlarm.getPointer();
//                pTemDiffInfo.write(0, pAlarmInfo.getByteArray(0, strThermDiffAlarm.size()), 0, strThermDiffAlarm.size());
//                strThermDiffAlarm.read();
//                String sThremDiffInfo = "通道号：" + strThermDiffAlarm.dwChannel + ",报警规则：" + strThermDiffAlarm.byAlarmRule + "，当前温差：" + strThermDiffAlarm.fCurTemperatureDiff;
//                System.out.println(sThremDiffInfo);
//                //可见光图片保存
//                if ((strThermDiffAlarm.dwPicLen > 0) && (strThermDiffAlarm.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_" + strThermDiffAlarm.fCurTemperatureDiff + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = strThermDiffAlarm.pPicBuff.getByteBuffer(offset, strThermDiffAlarm.dwPicLen);
//                        byte[] bytes = new byte[strThermDiffAlarm.dwPicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }
//                //热成像图片保存
//                if ((strThermDiffAlarm.dwThermalPicLen > 0) && (strThermDiffAlarm.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_" + "_ThermalDiffPiC" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = strThermDiffAlarm.pThermalPicBuff.getByteBuffer(offset, strThermDiffAlarm.dwThermalPicLen);
//                        byte[] bytes = new byte[strThermDiffAlarm.dwThermalPicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//
//                }
//                break;
//            case HCNetSDK.COMM_FIREDETECTION_ALARM://烟火检测
//                HCNetSDK.NET_DVR_FIREDETECTION_ALARM struFireDecAlarm = new HCNetSDK.NET_DVR_FIREDETECTION_ALARM();
//                struFireDecAlarm.write();
//                Pointer pFireDecAlarm = struFireDecAlarm.getPointer();
//                pFireDecAlarm.write(0, pAlarmInfo.getByteArray(0, struFireDecAlarm.size()), 0, struFireDecAlarm.size());
//                struFireDecAlarm.read();
//                String sFireDecAlarmInfo = "绝对时间：" + struFireDecAlarm.dwAbsTime + ",报警子类型：" + struFireDecAlarm.byAlarmSubType + ",火点最高温度 :" +
//                        struFireDecAlarm.wFireMaxTemperature + ",火点目标距离：" + struFireDecAlarm.wTargetDistance;
//                System.out.println(sFireDecAlarmInfo);
//                //可见光图片保存
//                if ((struFireDecAlarm.dwVisiblePicLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_FireDecAlarm" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struFireDecAlarm.pVisiblePicBuf.getByteBuffer(offset, struFireDecAlarm.dwVisiblePicLen);
//                        byte[] bytes = new byte[struFireDecAlarm.dwVisiblePicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }
//                //热成像图片保存
//                if ((struFireDecAlarm.dwPicDataLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_" + "_ThermalFireAlarm" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struFireDecAlarm.pBuffer.getByteBuffer(offset, struFireDecAlarm.dwPicDataLen);
//                        byte[] bytes = new byte[struFireDecAlarm.dwPicDataLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//
//                }
//                break;
            default:
                System.out.println("报警类型" + Integer.toHexString(lCommand));
                break;
        }
    }
}





